/**
 * 
 */
package it.fuesi.myseries.api.tasks;

import it.fuesi.myseries.api.AbstractApiTask;
import it.fuesi.myseries.model.MySeriesConstant;
import it.fuesi.myseries.model.Serie;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;

/**
 * @author fuesi
 *
 */
public class SearchSerie extends AbstractApiTask {


	@Override
	protected Map<String, String> getApiParams() {
		final String search = (String) getCurrentBundle().get(MySeriesConstant.SEARCH.toString());
		
		final Map<String,String> params = new HashMap<String,String>();
		params.put("searchString", search);
		
		System.out.println("SearchSerie getApiParams method");
		
		return params;
		
	}

	@Override
	protected Bundle processResults(JSONObject response) throws JSONException{
		final Bundle results = super.processResults(response);
		
		final Bundle data = new Bundle();
		
		if(results.getBoolean(MySeriesConstant.SUCCESS.toString())){
			final JSONArray array = response.getJSONArray("data"); //FIXME
			
			for(int i=0; i<array.length(); i++){
				final JSONObject obj = array.getJSONObject(i);
				Serie s = new Serie();
				s.setName(obj.getString("name")); //FIXME
				s.setId(obj.getInt("tvdbId")); //FIXME
				s.setOverview(obj.getString("overview")); //FIXME
				s.setBanner(obj.getString("banner")); //FIXME
				data.putSerializable(""+s.getId(), s);
			}
		}
		
		results.putBundle(MySeriesConstant.DATA.toString(), data);
		
		return results;
	}

}
